/**
 * by samsun
 * 显示业务流程视图
 */





function drawEpcModel(startx,starty,ctx,epcStore){
	 ctx.restore();
	 ctx.clearRect(0, 0,10000, 1500);
	 ctx.save();
	 ctx.translate(startx, starty);
	 var eAmount=epcStore.getCount();
     
	
     
     var eventImg=new Image();
     eventImg.onload=function(){ 
    	 
    	 var functionImg=new Image(); 
    	 functionImg.onload=function(){
    	   var orImg=new Image();
    	   orImg.onload=function(){
    	   var andImg=new Image();	   
    	   andImg.onload=function(){
    		var xorImg=new Image();   
    		xorImg.onload=function(){
    			var startImg=new Image(); 
    			startImg.onload=function(){
    				var endImg=new Image();
    				endImg.onload=function(){
    					var roleImg=new Image();
    					roleImg.onload=function(){
    						var i=0;
                			var record=epcStore.getAt(i);
                			var nextwide;
                			var nextlength;
                			var stack=new Array();       //为处理的节点
                			var stackIndex=new Array();
                      	    var next=new Array();  //
                      	    var position=new Array();      //下个节点的位置
                      	    var x,y,xc,yc,separator;
                      	    var mark=new Array(eAmount+1);
                      	    var markPosition=new Array(eAmount+1);
                      	    
                      	    if (record.get('type')=="start"){
                                    //画开始节点
                              
            	                     ctx.drawImage(startImg,0,10,30,30);
            	                  
                                    
                           
                             
                             for (var j=0;j<eAmount+1;j++){
                            	 markPosition[j]="0,0";
                            	 mark[j]=0;
                             }
                            
                             mark[0]=-1;
                          
                    
                      }
                      	  next=record.get('next');                    
                      	  
                          for (var j=0;j<next.length;j++){
                              var k=0;
                              var nextUnit=epcStore.getAt(k);              
                              while (k<eAmount && nextUnit.get('id')!=next[j]){
                        		      k++;
                        		      nextUnit=epcStore.getAt(k);   
                        	         }
                               if (k!=eAmount) {
                            	   stack.push(nextUnit); 
                            	   stackIndex.push(k);//记录ctx转移的坐标位置 便于下次绘图 
                            	   position.push(80);             //记录下个节点
                            	   position.push(j*200);
                            	   
                            	   if (position.length!=0 && mark[k]!=-1){
                                       y=position.pop();
                                       x=position.pop();
                                      
                                       
                                       if (y!=0) {
                                    		ctx.strokeStyle = "rgb(0, 0, 0)";
                                    		ctx.lineWidth = 1;
                                    		ctx.beginPath();
                                    		ctx.moveTo(15,40);
                                    		ctx.lineTo(15,y+25);
                                    		ctx.stroke();
                                    	    drawLine(30,y+25,80,y+25,ctx,0,0);
                                       }
                                       
                                       else drawLine(30,25,80,25,ctx,0,0);
                                       position.push(x);
                                       position.push(y);
                            	   
                               }
                               
                              }
                          
                     
                           
                           
                           }
                            while (i<eAmount && stack.length!=0){
                            	record=stack.pop();    
                            	
                            	  i++;
                            	  
                            	var index=stackIndex.pop();        //EPC 单元在数组epcStore的下标
                              if (record.get('type')=="event" ){
                            	  
                            	 
                            		  y=position.pop();
                            	      x=position.pop();
                            	      mark[index]=-1;
                            	      ctx.translate(x,y);
                            	      
                            	      updateToDrawPosition(position,x,y);            //更新待画图的单元的坐标
                            	   
                            	      updateDrawnPosition(markPosition,x,y);         //更新已画图的单元的坐标 
                            	      
                            	   markPosition[index]="0,0";                                  //画状态单元
                            	   drawUnit(ctx,0,0,'rgb(135, 206, 235)','rgb(255, 255, 240)',record);
                            	  
            			           ctx.drawImage(eventImg,3,15,20,20);
            			           ctx.fillStyle ="black";    
            		               ctx.font = "10pt Arial";
            			           ctx.fillText(record.get('name'),25,25,75);
            			           
            			           next==new Array();
            	                   next=record.get('next');
            	                   if (next.length==0 ){                                 //如果没有后继 则后继是结束节点
            	                	   if (mark[eAmount]!=-1){
            	                		   mark[eAmount]=-1;
                	                	   drawLine(100,25,150,25,ctx,0,0);
                	                	   ctx.drawImage(endImg,150,10,30,30);
                	                	   markPosition[eAmount]="150,10";
            	                	   }
            	                	   else {                                                    
            	                		   separator=markPosition[eAmount].indexOf(",");
                                 		   xc=parseInt(markPosition[eAmount].substring(0,separator));
                                 		   yc=parseInt(markPosition[eAmount].substring(separator+1,markPosition[eAmount].length));
                                 		   if (xc>0){
                                 			  ctx.strokeStyle = "rgb(0, 0, 0)";
      	                            		  ctx.lineWidth = 1;
      	                            		  ctx.beginPath();
      	                            		  ctx.moveTo(100,25);
      	                            		  ctx.lineTo(xc+15,25);
      	                            		  ctx.stroke();
      	                            		  if (yc<0) yc=yc+40; 
      	                             	      drawLine(xc+15,25,xc+15,yc,ctx,0,0);
                                 			   }
                                 		   else {
                                 		   if (yc<0) {
                                 				ctx.strokeStyle = "rgb(0, 0, 0)";
          	                            		ctx.lineWidth = 1;
          	                            		ctx.beginPath();
          	                            		ctx.moveTo(50,0);
          	                            		ctx.lineTo(50,yc+25);
          	                            		ctx.stroke();
          	                             	    drawLine(50,yc+25,xc+15,yc+25,ctx,0,0);
                                 				
                                 			}
                                 			else{
                                 				ctx.strokeStyle = "rgb(0, 0, 0)";
          	                            		ctx.lineWidth = 1;
          	                            		ctx.beginPath();
          	                            		ctx.moveTo(50,50);
          	                            		ctx.lineTo(50,y+25);
          	                            		ctx.stroke();
          	                             	    drawLine(50,y+25,xc+15,y+25,ctx,0,0);
                                 			} 
     
                                 		   }
            	                	   }
            	                	   
            	                   } 
            	          
            	                    
            	                   drawLineToNext(next,eAmount,stack,position,stackIndex,ctx,record,epcStore,mark,markPosition); 
                                    
                            	   
                            	
            			
            		         }
                            else if (record.get('type')=="function"){                   
                            	
                            		 y=position.pop();                                          //如果是待画图的单元是function
                               	     x=position.pop();
                               	     ctx.translate(x,y);
                               	     mark[index]=-1;
                               	     
                               	     updateToDrawPosition(position,x,y);            //更新待画图的单元的坐标
                           	   
                        	         updateDrawnPosition(markPosition,x,y);         //更新已画图的单元的坐标 
                        	      
                               	    markPosition[index]="0,0";
                			       
                			        drawUnit(ctx,0,0,'rgb(154, 205, 50)','rgb(255, 255, 240)',record);  //画function
                			        ctx.drawImage(functionImg,3,12,20,20);
                			       
                			        ctx.fillStyle ="black";    
                		            ctx.font = "10pt Arial";
                			        ctx.fillText(record.get('name'),25,25,75);
                			        
                			        next==new Array();
                                    next=record.get('next');
                                    
                                    var participant=record.get('participant');    //画参与者
                                    if (participant!=0){
                                    	  k=0;
                                          nextUnit=epcStore.getAt(k);
                                          while(k<eAmount&& nextUnit.get('id')!=participant){
                                          	k++;
                                          	nextUnit=epcStore.getAt(k);   
                                          }
                                          
                                          ctx.drawImage(roleImg,35,-75,30,30);
                                          drawLine(50,0,50,-45,ctx,0,0);
                                          ctx.fillStyle ="black";    
                      		              ctx.font = "10pt Arial";
                                          ctx.fillText(nextUnit.get('name'),25,-80);
                                    }
                                  
                                    
                                    drawLineToNext(next,eAmount,stack,position,stackIndex,ctx,record,epcStore,mark,markPosition);
                                   
             	              
                            	
                            	 
                            	
            			        
            		    }
                           
                            else if (record.get('type')=="connector" ){
                            	                          //待画图的单元是逻辑符
                            		y=position.pop();
                         	        x=position.pop();
                            	    ctx.translate(x,y);
                            	    mark[index]=-1;
                            	    
                            	    updateToDrawPosition(position,x,y);            //更新待画图的单元的坐标
                             	   
                          	        updateDrawnPosition(markPosition,x,y);         //更新已画图的单元的坐标
                          	        
                                  	markPosition[index]="0,0";
                                    if (record.get('name')=="xor")
                         	            ctx.drawImage(xorImg,0,10,30,30);
                                    else if(record.get('name')=="or")
                                    	ctx.drawImage(orImg,0,10,30,30);
                                    else ctx.drawImage(andImg,0,10,30,30);
                         	       next==new Array();
                                   next=record.get('next');
                                   
                                   drawLineToNext(next,eAmount,stack,position,stackIndex,ctx,record,epcStore,mark,markPosition);
 
                        	    
                            }
          
                    
                }
    						
    					};
    					roleImg.src='images/modelUnit/controlUnit/role.gif';
    					
    				}; 	
        			endImg.src='images/modelUnit/controlUnit/finish.gif';
    				
    			};
    			startImg.src='images/modelUnit/controlUnit/begin.gif';
    			
	
    	 };
    	 xorImg.src='images/modelUnit/controlUnit/xor.gif';
    	 };
    	 andImg.src='images/modelUnit/controlUnit/and.gif';
    	 };
    	 orImg.src='images/modelUnit/controlUnit/or.gif';
    	 };
    	 functionImg.src='images/modelUnit/controlUnit/function.gif';
     };
     eventImg.src='images/modelUnit/controlUnit/event.gif';
	     
	     
	     }

function drawLineToNext(next,eAmount,stack,position,stackIndex,ctx,record,epcStore,mark,markPosition){
	var weight,length,nextwide,nextlength;
	 if (record.get('type')!="connector"){
			 weight=50;
			 length=50;
		 }
		 else {
			 weight=15;
			 length=40;
		 }
    for (var j=0;j<next.length;j++){
        var k=0;
        var nextUnit=epcStore.getAt(k);
       while (k<eAmount && nextUnit.get('id')!=next[j]){
 		      k++;
 		      nextUnit=epcStore.getAt(k);   
 	         }
        stack.push(nextUnit);
        
        position.push(150);                //把等待画图的单元的坐标存入栈中
        position.push(j*200);
        
        if (stack.length!=0 && mark[k]!=-1){        //等待画图的单元未画过
          	var y=position.pop();
            var x=position.pop();
            stackIndex.push(k);
            
              if (y!=0) {
              	ctx.strokeStyle = "rgb(0, 0, 0)";
          		ctx.lineWidth = 1;
          		ctx.beginPath();
          		ctx.moveTo(weight,length);
          		ctx.lineTo(weight,y+25);
          		ctx.stroke();
           	    drawLine(weight,y+25,150,y+25,ctx,0,0);
              }
              
              else drawLine(weight*2,25,150,25,ctx,0,0);
        
             
              position.push(x);
              position.push(y);
          }
        else {                                        //等待画图的单元已画过
      	 nextUnit=stack.pop();
      	 position.pop();
   		 position.pop();
   		
   		 if (nextUnit.get('type')!="connector") {
   			  nextwide=50;
              nextlength=50;                    			 
   		 }
   		 else {
   			  nextwide=15;
   			  nextlength=40;
   		 }
   		 var separator=markPosition[k].indexOf(",");                        //取出已画图的单元的坐标
         var xc=parseInt(markPosition[k].substring(0,separator));
   		 var yc=parseInt(markPosition[k].substring(separator+1,markPosition[k].length));
   		
   			ctx.strokeStyle = "rgb(0, 0, 0)";
        	ctx.lineWidth = 1;
        		
        		
        	 if (yc<0)	{                                     //如果坐标在上方
        		ctx.beginPath();
        		ctx.moveTo(weight,50-length);                              
        		ctx.lineTo(weight,-100);
        		ctx.stroke();
        		
        		ctx.beginPath();
        		ctx.moveTo(weight,-100);
        		ctx.lineTo(xc+nextwide,-100);
        		ctx.stroke();
        		drawLine(xc+nextwide,-100,xc+nextwide,yc+nextlength,ctx,0,0);
        	 }
         	    
        	 else {
        		ctx.beginPath();                                       //如果坐标在下方
        		ctx.moveTo(weight,length);
        		ctx.lineTo(weight,90);
        		ctx.stroke();
        		
        		ctx.beginPath();
        		ctx.moveTo(weight,90);
        		ctx.lineTo(xc+nextwide,90);
        		ctx.stroke();
        		if (yc!=0)
        		drawLine(xc+nextwide,90,xc+nextwide,yc+50-nextlength,ctx,0,0);
        		else drawLine(xc+nextwide,90,xc+nextwide,yc+nextlength,ctx,0,0)
        	 }
      	  
        }
       }
}
function updateToDrawPosition(position,x,y){
	   var yc,xc;
	   for (var l=0;l<position.length;l=l+2){         //更新待画图的单元的坐标
		    yc=position[l+1];
		    xc=position[l];
		    yc=yc-y;
		    xc=xc-x;
	    	position[l]=xc;
		    position[l+1]=yc;
	       }
}
function updateDrawnPosition(markPosition,x,y){        //更新已画图的坐标
	  var yc,xc;
      for (var l=0;l<markPosition.length;l++){                    
	     separator=markPosition[l].indexOf(",");
	     xc=parseInt(markPosition[l].substring(0,separator))-x;
	     yc=parseInt(markPosition[l].substring(separator+1,markPosition[l].length))-y;
	     markPosition[l]=xc+","+yc;
  	
     }
}
function drawUnit(ctx,x,y,src,dst,record){
	 var gradient = ctx.createLinearGradient(0, 0, 0, 50);
	 gradient.addColorStop(0,src);
	 gradient.addColorStop(1,dst);
	 ctx.fillStyle = gradient;
	 ctx.fillRect( 0, 0, 100, 50); //渐变的背景色
	 
	 ctx.strokeStyle = '#000000'; 
	 ctx.beginPath(); 
	 ctx.rect(0.5,0.5, 100, 50);
	 ctx.stroke();              //黑边框
	 

}

